package cn.tedu.system.government.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.base.utils.CacheUtil;
import cn.tedu.system.government.mapper.GovernmentMapper;
import cn.tedu.system.government.pojo.dto.*;
import cn.tedu.system.government.pojo.entity.Policies;
import cn.tedu.system.government.pojo.entity.Video;
import cn.tedu.system.government.pojo.vo.PoliciesListVO;
import cn.tedu.system.government.pojo.vo.VideoVO;
import cn.tedu.system.government.service.GovernmentService;
import cn.tedu.system.product.pojo.dto.ProductDto;
import cn.tedu.system.product.pojo.dto.ProductIsAParam;
import cn.tedu.system.product.pojo.entity.Product;
import cn.tedu.system.product.pojo.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tedu.system.government.pojo.dto.UpdateAgricultureStatusDTO;

import java.io.*;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class GovernmentServiceImpl implements GovernmentService {
    @Autowired
    private GovernmentMapper governmentMapper;

    @Autowired
    private CacheUtil cacheUtil;

/*
* 下载附件
* */
@Override
public String getAttachmentUrlByPolicyId(Long policyId) {
    return governmentMapper.getAttachmentUrlByPolicyId(policyId);
}

    /*
* 增加助农故事浏览次数
* */

    @Override
    public void incrementVideoViewCount(Long videoId) {
        log.debug("增加助农故事浏览次数: {}", videoId);
        if (governmentMapper.incrementVideoViewCount(videoId) <= 0) {
            throw new ServiceException(StatusCode.FAILED, "增加浏览次数失败");
        }
        cacheUtil.del("video_Index"); // 清除缓存，下次重新加载最新数据

    }

    /*
    * 增加政策浏览次数
    * */
    @Override
    public void incrementPolicyViewCount(Integer policyId) {
        log.debug("增加政策浏览次数: {}", policyId);
        if (governmentMapper.incrementPolicyViewCount(policyId) <= 0) {
            throw new ServiceException(StatusCode.FAILED, "增加浏览次数失败");
        }
    }

    //添加宣传政策功能
    @Override
    public void setPolicies(PoliciesSetDto policiesSetDto) {
        log.debug("宣传政策业务: {}", policiesSetDto);
        Policies policies = new Policies();
        BeanUtils.copyProperties(policiesSetDto,policies);
        policies.setCreateTime(new Date());
        if (governmentMapper.set(policies)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }

    }

    //查询宣传政策功能
    @Override
    public List<PoliciesListVO> getList() {
        log.debug("查询宣传政策业务");
//        Serializable policiesIndex=cacheUtil.get("policies_Index");
//        if (policiesIndex!=null){
//            log.debug("数据来自Redis数据库...");
//            return (List<PoliciesListVO>) policiesIndex;
//            //return JsonResult.ok(policiesIndex);
//        }
        List<PoliciesListVO> list = governmentMapper.getList();
        if (list.size()==0 && list==null){
            throw new ServiceException(StatusCode.FAILED);
        }
//        cacheUtil.setExpire("policies_Index", (Serializable) list,60);
        return list;
    }

    //添加助农宣传故事
    @Override
    public void addVideo(VideoDto videoDto) {
        log.debug("助农宣传故事业务: {}",videoDto);

        Video video = new Video();
        BeanUtils.copyProperties(videoDto,video);
        video.setCreateTime(new Date());

        if (governmentMapper.addVideo(video)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //查看助农宣传故事
    @Override
    public List<VideoVO> getVideo() {
//        Serializable video=cacheUtil.get("video_Index");
//        if (video!=null){
//            log.debug("数据来自Redis数据库...");
//            return (List<VideoVO>)video;
//        }
        List<VideoVO> list = governmentMapper.getVideo();
        if (list.size()==0 && list==null){
            throw new ServiceException(StatusCode.FAILED);
        }
//        cacheUtil.setExpire("video_Index", (Serializable) list,60);
        return list;
    }



    // 新增一个方法：清除所有 promotion_Index 开头的缓存
    private void clearPromotionCache() {
        cacheUtil.delByPrefix("product_Index:");
    }


    //添加助农商品
    @Override
    public void addProduct(ProductIsAParam productDto) {
        log.debug("助农商品业务: {}", productDto);
        log.debug("Id: {}", productDto.getId());
        log.debug("name: {}", productDto.getName());
        if(productDto.getId()==null){
            throw new ServiceException(StatusCode.FAILED, "商品ID不能为空");
        }
        if(productDto.getName()==null){
            throw new ServiceException(StatusCode.FAILED, "商品名称不能为空");
        }
        if(productDto.getDescription()==null){
            throw new ServiceException(StatusCode.FAILED, "商品描述不能为空");
        }
        log.debug("商品业务: {}", productDto);
        Product product = new Product();
        BeanUtils.copyProperties(productDto, product);
        if (governmentMapper.addProduct(product) <= 0) {
            throw new ServiceException(StatusCode.FAILED,  "添加助农商品失败");
        }
        clearPromotionCache();
    }

    @Override
    public void reviseProduct(ProductDto promotionDto) {
        log.debug("助农商品修改业务: {}", promotionDto);
        Product product=new Product();
        BeanUtils.copyProperties(promotionDto,product);
        if (governmentMapper.reviseProduct(product) <= 0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }


      @Override
      public List<ProductVO> getList(Long id, Integer status) {
        log.debug("查询助农宣传商品业务");

        ProductGetDTO promotionGetDto = new ProductGetDTO();
        promotionGetDto.setId(id);
        promotionGetDto.setStatus(status);

        List<ProductVO> list = governmentMapper.getProductList(promotionGetDto);

        if (list == null || list.isEmpty()) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
      }


   @Override
   public void deleteProductById(Long id) {
        log.debug("删除商品: id={}", id);
        if (governmentMapper.deleteProductById(id) <= 0) {
            throw new ServiceException(StatusCode.FAILED);
        }
        clearPromotionCache(); // 清除所有缓存
    }

    @Override
    public List<PoliciesListVO> getPoliciesById(Long id) {
        List<PoliciesListVO> list = governmentMapper.getPoliciesById(id);
        if (list == null || list.isEmpty()) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }

    @Override
    public List<VideoVO> getStoryById(Long id) {
        List<VideoVO> list = governmentMapper.getStoryById(id);
        if (list == null || list.isEmpty()) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }


    @Override
    public ProductVO getProductById(Long id) {
        ProductVO productVO = governmentMapper.getProductById(id);
        if (productVO == null) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return productVO;
    }

    //修改助农商品
    @Override
    public List<ProductVO> getProductList() {
        log.debug("助农商品查询业务");
//        Serializable product=cacheUtil.get("product_Index");
//        if (product!=null){
//            log.debug("数据来自Redis数据库...");
//            return (List<ProductVO>)product;
//        }
        List<ProductVO> list = governmentMapper.getAdminProductList();
        if (list.size()==0 && list==null){
            throw new ServiceException(StatusCode.FAILED);
        }
//        cacheUtil.setExpire("product_Index", (Serializable) list,60);
        return list;
    }

    //加的功能
    @Override
    public void deletePolicy(Long policyId) {
        log.debug("删除助农政策业务: {}", policyId);
        if (governmentMapper.deletePolicyById(policyId) <= 0) {
            throw new ServiceException(StatusCode.FAILED, "删除助农政策失败");
        }
    }

    @Override
    public void deleteProduct(Long id) {
        log.debug("删除助农商品业务: {}", id);
        if (governmentMapper.deleteProductById(id) <= 0) {
            throw new ServiceException(StatusCode.FAILED, "删除助农商品失败");
        }
    }

    @Override
    public void deleteVideo(Long videoId) {
        log.debug("删除助农故事业务: {}", videoId);
        if (governmentMapper.deleteVideoById(videoId)<= 0) {
            throw new ServiceException(StatusCode.FAILED, "删除助农故事失败");
        }
    }

    @Override
    public void updatePolicy(PoliciesSetDto policiesSetDto) {
        log.debug("更新助农政策业务: {}", policiesSetDto);
        Policies policies = new Policies();
        BeanUtils.copyProperties(policiesSetDto, policies);
        policies.setCreateTime(new Date()); // 如果需要更新创建时间
        if (governmentMapper.updatePolicy(policies) <= 0) {

        }
    }

    @Override
    public void updateVideo(VideoDto videoDto) {
        log.debug("更新助农故事业务: {}", videoDto);
        Video video = new Video();
        BeanUtils.copyProperties(videoDto, video);
        video.setCreateTime(new Date()); // 如果需要更新创建时间
        if (governmentMapper.updateVideo(video) <= 0) {

        }
    }

    @Override
    public void updateAgricultureStatus(UpdateAgricultureStatusDTO dto) {
        log.debug("更新助农状态:{}", dto);
        governmentMapper.updateAgricultureStatus(dto);
    }

    @Override
    public void updateProductStatus(Long id, Integer status) {
        log.debug("更新商品状态: productId={}, status={}", id, status);
        UpdateProductStatusDTO dto = new UpdateProductStatusDTO();
        dto.setId(id);
        dto.setStatus(status);
        log.debug("dto" +dto);
        governmentMapper.updateProductStatus(dto);
    }
}
